package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.IsPhoneNum;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewPwdActivity extends BaseActivity {

    @BindView(R.id.new_pwd_phone)
    EditText newPwdPhone;
    @BindView(R.id.new_pwd_verify_code)
    EditText newPwdVerifyCode;
    @BindView(R.id.new_pwd_get_code)
    Button newPwdGetCode;
    @BindView(R.id.new_pwd_)
    EditText newPwd;
    @BindView(R.id.new_pwd_confirm)
    Button newPwdConfirm;
    int time = 60;
    String info, url, url_2;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                if (time >= 2) {
                    time -= 1;
                    newPwdGetCode.setText("稍后" + time + "s...");
                    handler.sendEmptyMessageDelayed(1, 1000);
                    newPwdGetCode.setEnabled(false);
                } else {
                    time = 60;
                    newPwdGetCode.setTextColor(getResources().getColor(R.color.colorPrimary));
                    newPwdGetCode.setEnabled(true);
                    newPwdGetCode.setText("获取验证码");
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_pwd);
        ButterKnife.bind(this);
        info = getIntent().getStringExtra("info");
        Log.e("uuuuuuuuuuuuuuu", "=" + info);

        if (info.equals("login")) {
            url = API.BaseApi_R;
            url_2 = API.MissPwd;
            newPwd.setHint("(6~16位字母或数字)");
        } else if (info.equals("tixian_l")) {
            url = API.BaseApi_L;
            url_2 = API.MissTixianPwd;
            newPwd.setHint("(6位数字密码)");

        } else if (info.equals("tixian_r")) {
            url = API.BaseApi_R;
            url_2 = API.MissTixianPwd;
            newPwd.setHint("(6位数字密码)");
        }
    }

    @Override
    public void initView() {
        super.initView();
    }


    @OnClick({R.id.back, R.id.new_pwd_get_code, R.id.new_pwd_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.new_pwd_get_code:
                if (IsPhoneNum.isMobileNO(newPwdPhone.getText() + "") == false) {
                    T.showShort(this, "请输入有效手机号码");
                } else {
                    if (info.equals("login")){

                        Log.e("rrrrrrrrrrrrr", "=" + url + "sendTXcheckCode");
                        Log.e("rrrrrrrrrrrrr", "=" + newPwdPhone.getText().toString().trim());
                        OkHttpUtils.get().url(API.BaseApi_R + "sendForgetcheckCode")
                                .addParams("uAccount", newPwdPhone.getText().toString().trim())
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(NewPwdActivity.this, "数据获取失败，请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("==============", "=" + response);
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            String status = jsonObject.getString("status") + "";
                                            if (status.equals("0")) {
                                                newPwdGetCode.setTextColor(getResources().getColor(R.color.colorGrey));
                                                newPwdGetCode.setText("稍后" + time + "s...");
                                                handler.sendEmptyMessageDelayed(1, 1000);
                                            } else {
                                                T.showShort(NewPwdActivity.this, jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });
                    }else {

                        Log.e("rrrrrrrrrrrrr", "=" + url + "sendTXcheckCode");
                        Log.e("rrrrrrrrrrrrr", "=" + newPwdPhone.getText().toString().trim());
                        OkHttpUtils.get().url(url + "sendTXcheckCode")
                                .addParams("uAccount", newPwdPhone.getText().toString().trim())
                                .addParams("token",SPUtils.get(NewPwdActivity.this,"token","")+"")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(NewPwdActivity.this, "数据获取失败，请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("==============", "=" + response);
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            String status = jsonObject.getString("status") + "";
                                            if (status.equals("0")) {
                                                newPwdGetCode.setTextColor(getResources().getColor(R.color.colorGrey));
                                                newPwdGetCode.setText("稍后" + time + "s...");
                                                handler.sendEmptyMessageDelayed(1, 1000);
                                            } else {
                                                T.showShort(NewPwdActivity.this, jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });
                    }

                }
                break;
            case R.id.new_pwd_confirm:
                Log.e("==============", "=-1" + url + url_2);
                if (IsPhoneNum.isMobileNO(newPwdPhone.getText() + "") == false) {
                    T.showShort(this, "请输入有效手机号码");
                } else {
                    if ((newPwdVerifyCode.getText() + "").equals("")) {
                        T.showShort(this, "短信验证码不能为空");
                    } else {
                        if ((newPwd.getText() + "").equals("")) {
                            T.showShort(this, "密码不能为空");
                        } else {
                            if ((newPwd.getText() + "").length() < 6 || (newPwd.getText() + "").length() > 16) {
                                T.showShort(this, "密码长度不小于6位且不大于16位");
                            } else {
                                if (info.equals("login")) {
                                    OkHttpUtils.get().url(url + url_2)
                                            .addParams("uAccount", newPwdPhone.getText().toString().trim())
                                            .addParams("cNumber", newPwdVerifyCode.getText().toString().trim())
                                            .addParams("uPassword", newPwd.getText().toString().trim())
                                            .build()
                                            .execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {
                                                    T.showShort(NewPwdActivity.this, "数据获取失败，请稍后再试");
                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("==============", "=" + response);
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        String status = jsonObject.getInt("status") + "";
                                                        if (status.equals("0")) {
                                                            finish();
                                                        }
                                                        T.showShort(NewPwdActivity.this, jsonObject.getString("msg"));

                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }

                                                }
                                            });

                                } else {
                                    //修改提现密码
                                    OkHttpUtils.get().url(url + url_2)
                                            .addParams("token", SPUtils.get(NewPwdActivity.this, "token", "") + "")
                                            .addParams("uAccount", newPwdPhone.getText().toString().trim())
                                            .addParams("cNumber", newPwdVerifyCode.getText().toString().trim())
                                            .addParams("uAccountpw", newPwd.getText().toString().trim())
                                            .build()
                                            .execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {
                                                    T.showShort(NewPwdActivity.this, "数据获取失败，请稍后再试");
                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("==============", "=2" + response);
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        String status = jsonObject.getInt("status") + "";
                                                        if (status.equals("0")) {
                                                            finish();
                                                        }
                                                        T.showShort(NewPwdActivity.this, jsonObject.getString("msg"));

                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }

                                                }
                                            });


                                }

                            }
                        }


                    }
                    break;
                }
        }
    }
}
